﻿using Business;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookManageSys.Admin
{
    public partial class OrderManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            OrdersBusiness ob = new OrdersBusiness();
            DataTable dt = ob.GetAllData();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            OrdersEntity oe = new OrdersEntity();
            oe.Username = TxtSearch.Text;

            OrdersBusiness ob = new OrdersBusiness();
            DataTable dt = ob.SearchAllData(oe);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "删除")
            {
                OrdersEntity oe = new OrdersEntity();
                oe.Orderid = Convert.ToInt32(e.CommandArgument);

                try
                {
                    OrdersBusiness ob = new OrdersBusiness();
                    int res = ob.DeleteOrder(oe);

                    if (res > 0)
                    {
                        Response.Write("<script>alert('删除成功！')</script>");
                        BindData();
                    }
                    else
                    {
                        Response.Write("<script>alert('删除失败！')</script>");
                    }
                }
                catch
                {
                    Response.Write("<script>alert('不可删除，存在主外键关联！')</script>");
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string sql = "";

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkSelect = (CheckBox)row.FindControl("ChkSelect");

                if (chkSelect.Checked)
                {
                    // 获取对应行的第2列数据
                    string data = row.Cells[1].Text; // 获取第2列数据
                    sql += " or orderid = " + data;
                }
            }

            try
            {
                OrdersBusiness ob = new OrdersBusiness();
                int res = ob.DeleteMoreOrders(sql);
                if (res > 0)
                {
                    Response.Write("<script>alert('删除成功')</script>");
                    BindData();
                }
                else
                {
                    Response.Write("<script>alert('删除失败')</script>");
                }
            }
            catch
            {
                Response.Write("<script>alert('不可删除，存在主外键关联！')</script>");
            }
        }

        protected void ChlAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkSelect = (CheckBox)row.FindControl("ChkSelect");
                if (ChkAll.Checked)
                {
                    chkSelect.Checked = true;
                }
                else
                {
                    chkSelect.Checked = false;
                }
            }
        }
    }
}